<?php
if(isset($_POST['btnsubmit']))
    {   
        $stuno=$_POST['stuno'];
        $pwd=$_POST['pwd'];

        require_once('conn.php');

        $sql="select * from student where studentid=$stuno and password=$pwd";

        $result=$db->query($sql);

        
        if($result->num_rows>=1)
        {    
            //使用Cookie保存登录的学号信息，7天后过期
            setcookie('stuno',$_POST['stuno'],time()+60*60*24*7);
            
            //使用session保存登录的学号信息
            session_start();
            $_SESSION['stuno']=$_POST['stuno'];

            $backurl='index.php';
            
            if(isset($_GET['frompage'])){
                $backurl=$_GET['frompage'].'.php';
            }
            echo "<script>window.location='{$backurl}'</script>";
        }
        else{
            echo "<script>window.alert('用户名或密码错误！')</script>";
        }
        
    }
?> 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style type="text/css">
#login{
    width:300px;
    border:1px solid blue;
    line-height:40px;
    margin:0 auto;
    padding-left:50px;
    padding-top:15px;
    padding-bottom:15px;
    text-align:left;
    font-size:14px;
}
.error{
    color:red;
}
</style>
</head>
<body>
<?php 
    include  'HeaderNav.html';  
?>
<h1>用户登录</h1>
<form action="" method="post">
<div id="login">
    <div>
学号：<input type="text" name="stuno"  value="
<?php
   if(isset($_COOKIE['stuno'])){
    echo $_COOKIE['stuno'];
   }
?>"><span class="error">*</span>
    </div>
    <div>
密码：<input type="password" name="pwd"><span class="error">*</span>
    </div>
    <div style="margin-left:85px;">
        <input type="submit" name="btnsubmit" value="登录">
    </div>
</div>
</form>
<?php 
    include  'Footer.html';    
?>
</body>
</html>
